Skip to content

Conversation

@kimberlythegeek
Copy link
Contributor

@kimberlythegeek kimberlythegeek commented Oct 4, 2018

building off of #144 by @matt-l-w

fixed mock import error

@kimberlythegeek
Copy link
Contributor Author

@matt-l-w r?

@neverstew
Copy link
Contributor

Doesn't look like the Unicode branch will get tested - might be nice to add in a case for that... Otherwise looks good!



@pytest.fixture
def tempdir():
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytest provides a handy tmpdir fixture

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

try:
f.write(unicode(json.dumps(data, indent=4)))
except NameError:
f.write(json.dumps(data, indent=4))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using json.dumps try json.dump as this returns a file-like object and shouldn't require the try/except.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm running into an error with py27 when using json.dump

TypeError: write() argument 1 must be unicode, not str

def test_write_results_to_file(tmpdir, mocker):
    axe = Axe(mocker.MagicMock())
    data = {"testKey": "testValue"}
    filename = path.join(str(tmpdir), "results.json")
    axe.write_results(data, filename)
def write_results(self, data, name="results.json"):
    """
    Write JSON to file with the specified name.
    :param name: Name of file to be written to.
    :param output: JSON object.
    """
    with open(name, "w", encoding="utf8") as f:
        json.dump(data, f)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, sorry - that sucks. You could consider using futures or six to make this code look a little nicer, but I'm not sure if it's worth the additional dependency.

try:
from unittest.mock import MagicMock
except ImportError:
from mock import MagicMock
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to consider https://github.com/pytest-dev/pytest-mock/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@kimberlythegeek kimberlythegeek merged commit a73dbb2 into django-commons:master Oct 8, 2018
@kimberlythegeek kimberlythegeek deleted the feature/fix_write_method branch October 8, 2018 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants